
/**
 * Project Name:cats-core-impl
 * File Name:XzqhServiceImpl.java
 * Package Name:com.catsic.core.user.service
 * Date:2014年2月27日上午11:26:52
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.pub.xzqh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.pub.condition.XzqhCondition;
import com.catsic.pub.constants.Constants;
import com.catsic.pub.convert.Converts;
import com.catsic.pub.entity.Xzqh;
import com.catsic.pub.exception.CatsicExceptionCode;
import com.catsic.pub.form.XzqhForm;
import com.catsic.pub.specification.XzqhSpec;
import com.catsic.pub.xzqh.dao.XzqhDao;
import com.catsic.pub.xzqh.service.XzqhService;

/**
 * ClassName: XzqhServiceImpl
 * Description: 行政区划Service实现类
 * date: 2014年2月27日 上午11:26:52
 * author: yangyd
 */
@Service("xzqhService")
public class XzqhServiceImpl extends BaseServiceImpl<Xzqh, String> implements XzqhService{
	
	/**
	 * xzqhDao:行政区划Dao
	 */
	@Autowired
	private XzqhDao xzqhDao;
	
	/**
	 * @see com.catsic.core.base.BaseServiceImpl#add(java.lang.Object)
	 */
	@Override
	public XzqhForm add(XzqhForm form) {
		Xzqh xzqh = new Xzqh();
		xzqh.setName(form.getName());
		//xzqh.setJb(form.getJb());
		if(StringUtils.isNotBlank(form.getPid())){
			xzqh.setParent(findById(form.getPid()));
		}
		return Converts.convert(super.add(xzqh));
	}
	
	/**
	 * @see com.catsic.core.xzqh.service.XzqhService#update(com.catsic.core.entity.Xzqh)
	 */
	@Override
	public XzqhForm update(XzqhForm form) {
		Xzqh xzqh = findById(form.getId());
		xzqh.setName(form.getName());		
		return Converts.convert(super.add(xzqh));
	}
	
	/**
	 * @see com.catsic.core.xzqh.service.XzqhService#findById(java.lang.Long)
	 */
	@Override
	public Xzqh findById(String id){
		Xzqh xzqh = getById(id);
		if(null == xzqh){
			throw new CatsicException(CatsicExceptionCode.实体不存在,Constants.XZQH);
		}
		return xzqh;
	}
	
	/**
	 * @see com.catsic.core.xzqh.service.XzqhService#findXzqhById(java.lang.String)
	 **/
	@Override
	public XzqhForm findXzqhById(String id){
		return Converts.convert(findById(id));
	}
	
	/**
	 * @see com.catsic.core.xzqh.service.XzqhService#findAll(java.lang.Long)
	 */
	@Override
	public List<XzqhForm> findAll(XzqhCondition condition){
		XzqhSpec spec = new XzqhSpec(condition);
		List<XzqhForm> treelist = new ArrayList<XzqhForm>();
		List<Xzqh> list = xzqhDao.findAll(spec);
		for (Xzqh xzqh : list) {
			treelist.add(Converts.convert(xzqh));
		}
		return treelist;
	}
}
